package com.rock.code.province.hn.coll;

import com.rock.code.common.util.SnmpClient;
import com.rock.code.common.util.SpringContextUtil;
import com.rock.code.common.util.WriteFile;
import com.rock.code.common.util.WriteKPI;
import com.rock.code.datacollconfig.buildenv.dao.TbCollConfSnmpDao;
import com.rock.code.datacollconfig.buildenv.domain.TbCollConfSnmp;
import com.rock.code.datacollconfig.coll.interfaces.CollDataItemInterface;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author zhangli
 * @create 2019/3/12 16:09
 **/
public class BuildCollImpl implements CollDataItemInterface {

    @Resource
    TbCollConfSnmpDao tbCollConfSnmpDao;

    @Override
    public void execute(String[] args) throws Exception {

        tbCollConfSnmpDao = (TbCollConfSnmpDao) SpringContextUtil.getBeanByClass(TbCollConfSnmpDao.class);

        HashMap pramMap = new HashMap();
        pramMap.put("enable", "1");
        pramMap.put("dev_type", "0");
        List<TbCollConfSnmp> _list = tbCollConfSnmpDao.getAllCollSnmpConf(pramMap);

        //snmpclient 放入缓存
        HashMap<String, SnmpClient> cacheMap = new HashMap<>();
        for (TbCollConfSnmp tbCollConfSnmp : _list) {
            String snmp_ip = tbCollConfSnmp.getSnmp_ip();
            if (cacheMap.get(snmp_ip) == null || cacheMap.isEmpty()) {
                cacheMap.put(tbCollConfSnmp.getSnmp_ip(), getSnmpClient(tbCollConfSnmp));
            }

        }

        for (TbCollConfSnmp tbCollConfSnmp : _list) {
            System.out.println(tbCollConfSnmp.getSnmp_name());//采集snmp的指标信息

            String ip = tbCollConfSnmp.getSnmp_ip();

            // snmp 的snmpclient
            SnmpClient snmpClient = getSnmpClient(tbCollConfSnmp);

            if (snmpClient == null) {
                continue;
            }

            String kbp = tbCollConfSnmp.getKbp();
            String path = tbCollConfSnmp.getPath();

            doBuildEnvData(tbCollConfSnmp, snmpClient);
        }

        System.out.println("===============zhangli+++++++++++++===========");


    }

    private SnmpClient getSnmpClient(TbCollConfSnmp tbCollConfSnmp) {
        String ip = tbCollConfSnmp.getSnmp_ip();
        String community = tbCollConfSnmp.getCommunity();
        String snmp_version = tbCollConfSnmp.getSnmp_version();
        String securitylevel = tbCollConfSnmp.getSecuritylevel();
        String port = tbCollConfSnmp.getSnmp_port();
        String user = tbCollConfSnmp.getSnmp_user();
        String pwd = tbCollConfSnmp.getSnmp_pwd();
        String method = tbCollConfSnmp.getMethod();
        String protocol = tbCollConfSnmp.getProtocol();
        String passphrase = tbCollConfSnmp.getPassphrase();
        SnmpClient snmp = null;
        if ("v2c".equals(snmp_version)) {
            snmp = SnmpClient.create("v2c")
                    .setHost(ip)
                    .setPort(port)
                    .setCommunity(community);
        } else if ("3".equals(snmp_version)) {

            if ("authNoPriv".equals(securitylevel)) {
                snmp = SnmpClient.create("3")
                        .setHost(ip)
                        .setPort(port)
                        .setSerurityName(user)
                        .setAuthenticationProtocol(protocol)
                        .setAuthenticationPassphrase(passphrase)
                        .setSecurityLevel(securitylevel);
            } else if ("authPriv".equals(securitylevel)) {
                snmp = SnmpClient.create("3")
                        .setHost(ip)
                        .setPort(port)
                        .setSerurityName(user)
                        .setAuthenticationProtocol(protocol)
                        .setAuthenticationPassphrase(pwd)
                        .setPrivacyProtocol(protocol)
                        .setPrivacyPassphrase(passphrase)
                        .setSecurityLevel(securitylevel);
            } else if ("noAuthNoPriv".equals(securitylevel)) {
                snmp = SnmpClient.create("3")
                        .setHost(ip)
                        .setPort(port)
                        .setSerurityName(user)
                        .setSecurityLevel(securitylevel);
            }

        }
        return snmp;


    }

    private void doBuildEnvData(TbCollConfSnmp tbCollConfSnmp, SnmpClient snmpClient) {
        if (snmpClient == null) {
            return;
        }
        WriteKPI writeKPI = new WriteKPI();
        String dataTime = writeKPI.getNowTime();
        String kpi_id = tbCollConfSnmp.getKpi_id();
        String kbp = tbCollConfSnmp.getKbp();
        String path = tbCollConfSnmp.getPath();
        String oid = tbCollConfSnmp.getSnmp_oid();
        String resource_type = tbCollConfSnmp.getResource_type();
        String snmp_ip = tbCollConfSnmp.getSnmp_ip();

        String kbp_id = kbp + ":" + snmp_ip.replace(".", "_") + "-total";


        String kpi_value = null;
        try {
            snmpClient.connect();
            kpi_value = snmpClient.get(oid);
            snmpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
            kpi_value = "null";
        }


        writeKPI.writeKPItoList(kpi_value, kpi_id, kbp_id, dataTime, resource_type);
        WriteFile wf = new WriteFile();
        wf.writeSevenStrucFile(path, writeKPI.getVECTOR());

    }
}
